<template>
	<view class="setPsw">
		<topTitle />
		<view class="title">
			<view>{{ $t('enterPsw.payment_password') }}</view>
			<view>{{ $t('enterPsw.enter_payment_password') }}</view>
		</view>
		<view class="ipt">
			<view>
				<view>{{ $t('enterPsw.payment_password') }}</view>
				<up-input :placeholder="$t('enterPsw.please_enter_password')" maxlength="6" v-model="payment_password"
					v-number-only clearable :password="eye" class="item" border="none">
					<template #suffix>
						<i @click="eye = !eye" :class="eye ? 'hideEye' : 'showEye'"></i>
					</template>
				</up-input>
			</view>
		</view>
		<up-button class="btn" :text="$t('enterPsw.confirm_submit')" @click="saveFn"></up-button>
	</view>
</template>


<script setup lang="ts">
	import { ref, getCurrentInstance } from "vue";
	const instance = getCurrentInstance()?.appContext.config.globalProperties;

	import { onLoad, onShow } from "@dcloudio/uni-app";
	import { WisdomProjectBuy } from "@/api/ProjectApi";
	import { UserWithdrawal, UserFundSwitching, UserInfo } from "@/api/UserApi";

	let eye = ref<boolean>(true)
	let type = ref<string>()
	let id = ref<string>()
	let title = ref<string>('')
	let obj = ref<any>({})
	let payment_password = ref<any>()

	onLoad((options : any) => {

		obj.value = options
		type.value = options.type
		if (options.title) {
			title.value = options.title

		}

	});
	onShow(() => {
		UserInfo().then((e) => {
			if (!e.payment_password_set) {
				uni.showModal({
					content: instance?.$t('enterPsw.set_payment_password'),
					confirmText: instance?.$t('enterPsw.confirm'),
					cancelText: instance?.$t('enterPsw.cancel'),
					success: (res) => {
						if (res.confirm) {
							instance?.goRedirectTo('/pages/mine/editPayPsw')
						} else {
							instance?.goBack(1)
						}
					}
				})

			}
		})


	});

	const saveFn = async () => {
		if (!payment_password.value) {
			uni.showToast({
				title: instance?.$t('enterPsw.please_enter_password'),
				icon: "none"
			})
			return
		}
		if (payment_password.value.length !== 6) {
			uni.showToast({
				title: instance?.$t('enterPsw.password_six_digits'),
				icon: "none"
			})
			return
		}
		if (type.value == 'intelligentInvestment') {
			await WisdomProjectBuy({ id: obj.value?.id, payment_password: instance?.getRsaCode(payment_password.value) })
		}
		if (type.value == 'withdrawal') {
			await UserWithdrawal({ wallet_address: obj.value.wallet_address, money: obj.value.money, payment_password: instance?.getRsaCode(payment_password.value) })
		}
		if (type.value == 'transfer') {
			await UserFundSwitching({ switch_type: obj.value.switch_type, money: obj.value.money, payment_password: instance?.getRsaCode(payment_password.value) })
		}
		instance?.goRedirectTo('/pages/index/completed?type=' + type.value + '&title=' + title.value || "")
	}
</script>
<style scoped lang="scss">
	@import "../../static/scss/setUp.scss";
</style>